package com.timemanage.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import com.timemanage.R
import com.timemanage.dao.AppDatabase
import com.timemanage.dao.UserDao
import com.timemanage.utils.MyApplication
import kotlinx.android.synthetic.main.activity_login.*

class loginActiviy : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        var userName=""
        var pwd=""
        var database= AppDatabase.getDatabase(MyApplication.context)
        var userdao: UserDao =database.userDao();
        var users = userdao.selectAcUser()
        if(users!=null){
            val intent = Intent(this, BottomBar::class.java)
            intent.putExtra("id",1)
            startActivity(intent)
            finish()
        }
        if(intent.hasExtra("userName") && intent.hasExtra("pwd")){
            userName = intent.getStringExtra("userName").toString()
            pwd      = intent.getStringExtra("pwd").toString()
            username.setText(userName)
            password.setText(pwd)
        }
        signup.setOnClickListener{
            val intent = Intent(this, sginupActivity::class.java)
            startActivity(intent)
        }
//        var butLogin = findViewById<Button>(R.id.login)
//
        login.setOnClickListener{
            Log.d("点击成功","aaaaa")
            val tname = username.text.toString()
            val tpwd  = password.text.toString()
            var database= AppDatabase.getDatabase(MyApplication.context)
            var userdao: UserDao =database.userDao();
            var users = userdao.selectUser()
            var f=0
            for(i in users){
                if(tname == i.useName && tpwd == i.userPd){
                    f=1
                    break
                }
            }
            if(f==1) {
                userdao.UpdateActive(tname)
                val intent = Intent(this, BottomBar::class.java)
                startActivity(intent)
                finish()
            }else{
                Toast.makeText(this,"账号或密码不正确",Toast.LENGTH_SHORT).show()
            }
        }

    }
}